package com.study.es.area.controller;

import com.alibaba.fastjson.JSON;
import com.study.es.area.entity.Area;
import com.study.es.area.service.AreaService;
import com.study.es.base.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "area")
public class AreaController {
   
   @Autowired
   private ElasticsearchTemplate elasticSearchTemplate;
   @Autowired
   private AreaService areaService;

   private static final String URL = "jdbc:mysql://127.0.0.1:3306/springboot_es?characterEncoding=UTF-8";
   private static final String USERNAME = "root";
   private static final String PASS = "password";


   @GetMapping(value="index")
   public String  index() {
		 return "area/index";
   }


   @PostMapping(value="list")
   public @ResponseBody
   Pages<Area> list(Integer pageNumber, Integer pageSize, String searchContent) {
      Pages<Area> result = null;
      if(searchContent==null || searchContent.trim().equals("")){
         result =  areaService.searchAreaPage(pageNumber, pageSize, searchContent);
      } else {
         result =  areaService.queryHitByPage(pageNumber, pageSize, searchContent, "area","mergerName");
      }
      System.out.println(JSON.toJSONString(result));
      return result;
   }


   @GetMapping(value = "initData")
   public @ResponseBody String initData(){
      long start = System.currentTimeMillis();
      List<Area> areaList = readSceneList();
      for (Area area : areaList) {
         areaService.saveArea(area);
      }
      long end = System.currentTimeMillis();
      return "success \r\n bulkIndex completed use time: "+ (end-start);
   }

   @GetMapping(value = "deleteData")
   public @ResponseBody String testDelete(){
      List<Area> areaList = readSceneList();
      for(Area area : areaList){
         areaService.deleteArea(area);
      }
      return "success";
   }

   @GetMapping(value = "createIndex")
   public @ResponseBody String createIndex(){
      elasticSearchTemplate.createIndex(Area.class);
      elasticSearchTemplate.putMapping(Area.class);
      return "success";
   }


   @GetMapping(value = "deleteIndex")
   public @ResponseBody String deleteIndex(){
      Boolean success = elasticSearchTemplate.deleteIndex(Area.class);
      return success.toString();
   }




   public List<Area> readSceneList(){
      List<Area> areaList = new ArrayList<Area>();
      Connection conn = null;
      Statement stmt = null;
      ResultSet rs = null;
      try{
         conn = JDBCConnect.getConnect(URL,USERNAME,PASS);
         stmt = conn.createStatement();
         String sql = "SELECT * FROM sh_area";
         rs = stmt.executeQuery(sql);
         while(rs.next()){
            Area area = new Area();
            area.setId(rs.getLong(1));
            area.setPid(rs.getLong(2));
            area.setShortname(rs.getString(3));
            area.setName(rs.getString(4));
            area.setMergerName(rs.getString(5));
            area.setLevel(rs.getShort(6));
            area.setPinyin(rs.getString(7));
            area.setCode(rs.getString(8));
            area.setZipCode(rs.getString(9));
            area.setFirst(rs.getString(10));
            area.setLocation(rs.getString(12)+","+rs.getString(11));
            areaList.add(area);
         }
      }catch(Exception e){
         e.printStackTrace();
      }finally{
         if(rs!=null){
            try {
               rs.close();
            } catch (SQLException e) {
               e.printStackTrace();
            }
         }
         if(stmt!=null){
            try {
               stmt.close();
            } catch (SQLException e) {
               e.printStackTrace();
            }
         }
         if(conn!=null){
            try {
               conn.close();
            } catch (SQLException e) {
               e.printStackTrace();
            }
         }
      }
      return areaList;
   }
}
